package com.ds.lens.data.controller;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ds.lens.data.common.client.vo.LensMetricInVO;
import com.ds.lens.data.service.LensMetricService;
import com.ds.lens.data.service.LensQueryService;
import com.ds.lens.data.vo.BaseOutVO;
import com.ds.lens.data.vo.factory.VOFactory;
import com.ds.lens.data.vo.in.v2.metric.LensMetricQueryInVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author WeiShaoying
 * @date 2019/11/8 下午2:42
 */
@RestController
@Api(value = "Lens metric controller")
@RequestMapping(produces = {"application/json;charset=UTF-8"})
@Slf4j
public class LensMetricController {

    @Autowired
    LensMetricService lensMetricService;

    @Autowired
    LensQueryService lensQueryService;

    private Gson gson = new GsonBuilder().create();

    @ApiOperation(value = "mark dynamic detection result metric")
    @PostMapping(value = "/lens/metric/dynamic/detection/result/mark")
    public BaseOutVO markDynamicDetectionResult(@RequestBody List<String> detectionResultIds) {
        lensQueryService.markDynamicDetectionResult(detectionResultIds);
        return VOFactory.getSuccessBaseOutVO();
    }

    @ApiOperation(value = "query dynamic detection result metrics")
    @PostMapping(value = "/lens/metric/dynamic/query")
    public BaseOutVO queryMetrics(@RequestBody @Valid LensMetricQueryInVO inVO) {
        return VOFactory.getSuccessBaseOutVO(lensQueryService.queryDynamicDetectionResults(inVO.getMonitorId(), inVO.getVersion(),
                inVO.getStartTime(), inVO.getEndTime(), inVO.getMetricBy()));
    }

    @ApiOperation(value = "query metrics")
    @PostMapping(value = "/lens/metric/query")
    public BaseOutVO queryMetrics(@RequestBody @Valid LensMetricInVO inVO) {
        log.info("/lens/metric/query input is " + gson.toJson(inVO));
        return VOFactory.getSuccessBaseOutVO(lensQueryService.query(inVO));
    }

    @ApiOperation(value = "Query metric names")
    @GetMapping(value = "/lens/metric/names")
    public BaseOutVO getMetricNames(@RequestParam("integrationId") Long integrationId,
                                    @RequestParam("applicationId") Long applicationId) {
        return VOFactory.getSuccessBaseOutVO(lensMetricService.queryMetricNamesByIntegrationId(integrationId, applicationId));
    }
}
